#include "messager.h"
#include "ams.h"
#include "log.h"
#include "lvgl.h"
#include "rtc_misc.h"

LV_IMG_DECLARE(icon_info);

LV_IMG_DECLARE(dial0_bg);
LV_IMG_DECLARE(dial0_black_bg);
LV_IMG_DECLARE(dial0_hours);
LV_IMG_DECLARE(dial0_hum_press_center);
LV_IMG_DECLARE(dial0_hum_press_needle);
LV_IMG_DECLARE(dial0_needle_center);
LV_IMG_DECLARE(dial0_seconds);
LV_IMG_DECLARE(dial0_minutes);

ACTIVITY_OPS_IMPLEMENT_DECLARE(dial, &icon_info, ACTIVITY_ATTR_FLAG_HOME);

static lv_obj_t *img_dial_bg = NULL;
static lv_obj_t *img_dial_black_bg = NULL;
static lv_obj_t *img_dial_hours = NULL;
static lv_obj_t *img_dial_hum_center = NULL;
static lv_obj_t *img_dial_press_center = NULL;
static lv_obj_t *img_dial_hum_needle = NULL;
static lv_obj_t *img_dial_press_needle = NULL;
static lv_obj_t *img_dial_needle_center = NULL;
static lv_obj_t *img_dial_seconds = NULL;
static lv_obj_t *img_dial_minutes = NULL;


/**
 * @description:
 * @param {void} *img_hours
 * @param {void} *img_minutes
 * @param {void} *img_seconds
 * @param {void} *img_hum_needle
 * @param {void} *img_press_needle
 * @return {*}
 */
static void app_dial_set_time(void *img_hours, void *img_minutes, void *img_seconds, void *img_hum_needle, void *img_press_needle)
{
    rtc_struct_t rtc_struct;
    rtc_time_get(&rtc_struct);
    lv_img_set_angle(img_hours, (rtc_struct.hour % 12) * 300 + rtc_struct.minutes * 5);
    lv_img_set_angle(img_minutes, rtc_struct.minutes * 60);
    lv_img_set_angle(img_seconds, rtc_struct.seconds * 60);
    lv_img_set_angle(img_hum_needle, rtc_struct.seconds * 30 + 2700);
    lv_img_set_angle(img_press_needle, rtc_struct.seconds * 30 + 2700);
    log_info("%d %d %d, %d:%d:%d\r\n", rtc_struct.year, rtc_struct.month, rtc_struct.day, rtc_struct.hour, rtc_struct.minutes, rtc_struct.seconds);
}


// ##
static void app_start(void *content)
{
}

// ##
static void app_stop(void *content)
{
}

// ##
static void app_prepare(void *content)
{
    ams_activity_t *activity = (ams_activity_t *)content;
    AMS_UNUSE(activity);
}

// ##
static void *app_create(void *content)
{
    ams_activity_t *activity = (ams_activity_t *)content;
    activity->ui = lv_obj_create(NULL, NULL);
    img_dial_black_bg = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_black_bg, &dial0_black_bg);
    lv_obj_set_pos(img_dial_black_bg,
                    (LV_HOR_RES_MAX - lv_obj_get_width(img_dial_black_bg)) / 2,
                    (LV_VER_RES_MAX - lv_obj_get_height(img_dial_black_bg)) / 2);

    img_dial_bg = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_bg, &dial0_bg);
    lv_obj_set_pos(img_dial_bg,
                    (LV_HOR_RES_MAX - lv_obj_get_width(img_dial_bg)) / 2,
                    (LV_VER_RES_MAX - lv_obj_get_height(img_dial_bg)) / 2);

    img_dial_needle_center = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_needle_center, &dial0_needle_center);
    lv_obj_set_pos(img_dial_needle_center,
                    (LV_HOR_RES_MAX - lv_obj_get_width(img_dial_needle_center)) / 2,
                    (LV_VER_RES_MAX - lv_obj_get_height(img_dial_needle_center)) / 2);

    img_dial_hours = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_hours, &dial0_hours);
    lv_obj_set_pos(img_dial_hours, (LV_HOR_RES_MAX / 2) - 12, (LV_VER_RES_MAX / 2) - 106);
    lv_img_set_pivot(img_dial_hours, 12, 106);

    img_dial_minutes = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_minutes, &dial0_minutes);
    lv_obj_set_pos(img_dial_minutes, (LV_HOR_RES_MAX / 2) - 12, (LV_VER_RES_MAX / 2) - 134);
    lv_img_set_pivot(img_dial_minutes, 12, 134);

    img_dial_seconds = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_seconds, &dial0_seconds);
    lv_obj_set_pos(img_dial_seconds, (LV_HOR_RES_MAX / 2) - 2, (LV_VER_RES_MAX / 2) - 172);
    lv_img_set_pivot(img_dial_seconds, 2, 172);

    img_dial_hum_center = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_hum_center, &dial0_hum_press_center);
    lv_obj_set_pos(img_dial_hum_center, 92, 184);

    img_dial_press_center = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_press_center, &dial0_hum_press_center);
    lv_obj_set_pos(img_dial_press_center, 276, 184);

    img_dial_hum_needle = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_hum_needle, &dial0_hum_press_needle);
    lv_obj_set_pos(img_dial_hum_needle, (LV_HOR_RES_MAX *3 / 4) - 8, (LV_VER_RES_MAX / 2) - 38);
    lv_img_set_pivot(img_dial_hum_needle, 3, 38);

    img_dial_press_needle = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_press_needle, &dial0_hum_press_needle);
    lv_obj_set_pos(img_dial_press_needle, (LV_HOR_RES_MAX / 4) + 3, (LV_VER_RES_MAX / 2) - 38);
    lv_img_set_pivot(img_dial_press_needle, 3, 38);

    app_dial_set_time(img_dial_hours, img_dial_minutes, img_dial_seconds, img_dial_hum_needle, img_dial_press_needle);

    return activity->ui;
}

// ##
static void *app_destroy(void *content)
{
    return NULL;
}

// ##
static void app_event_handle(void *event)
{
    ams_event_t *evt = event;
    switch (evt->msg_id)
    {
        case MSG_RTC_ALARM:
        {
            app_dial_set_time(img_dial_hours, img_dial_minutes, img_dial_seconds, img_dial_hum_needle, img_dial_press_needle);
            break;
        }
        default:
        {
            log_info("unknown msg : %d", evt->msg_id);
            break;
        }
    }
}

// ##
static void app_gesture_handle(void *event)
{
    ams_gesture_t evt = *((ams_gesture_t *)event);
    switch(evt)
    {
        case GESTURE_SLIDE_TO_TOP:
            break;
        case GESTURE_SLIDE_TO_BOTTOM:
            break;
        case GESTURE_SLIDE_TO_LEFT:
            ams_switch_by_name("applist", DIR_SLIDE_TO_LEFT);
            break;
        case GESTURE_SLIDE_TO_RIGHT:
            break;
    }
}
